package com.wjs.spring.bean;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class test {
    public static void main(String[] args) throws Exception {
        String className = "com.wjs.spring.bean.Student";
        String propertyName = "age";
        //已知类名和属性名，通过反射实现为属性赋值
        //获取类
        Class<?> aClass = Class.forName(className);
        //创建类的实例
        Constructor<?> constructor = aClass.getConstructor();
        Object o = constructor.newInstance();
        //获取set方法
        String methodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
        Method declaredMethod = aClass.getDeclaredMethod(methodName, int.class);
        //调用set方法为属性赋值
        declaredMethod.invoke(o, 18);
        System.out.println(o);
    }
}
